home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tclBasic.c < prev    next >
C/C++ Source or Header  |  1993-02-13  |  29KB  |  1,071 lines

  1. /* 
  2.  * tclBasic.c --
  3.  *
  4.  *    Contains the basic facilities for TCL command interpretation,
  5.  *    including interpreter creation and deletion, command creation
  6.  *    and deletion, and command parsing and execution.
  7.  *
  8.  * Copyright 1987-1992 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #include "tclInt.h"
  19.  
  20. /*
  21.  * The following structure defines all of the commands in the Tcl core,
  22.  * and the C procedures that execute them.
  23.  */
  24.  
  25. typedef struct {
  26.     char *name;            /* Name of command. */
  27.     Tcl_CmdProc *proc;        /* Procedure that executes command. */
  28. } CmdInfo;
  29.  
  30. /*
  31.  * Built-in commands, and the procedures associated with them:
  32.  */
  33.  
  34. static CmdInfo builtInCmds[] = {
  35.     /*
  36.      * Commands in the generic core:
  37.      */
  38.  
  39.     {"append",        Tcl_AppendCmd},
  40.     {"array",        Tcl_ArrayCmd},
  41.     {"break",        Tcl_BreakCmd},
  42.     {"case",        Tcl_CaseCmd},
  43.     {"catch",        Tcl_CatchCmd},
  44.     {"concat",        Tcl_ConcatCmd},
  45.     {"continue",    Tcl_ContinueCmd},
  46.     {"error",        Tcl_ErrorCmd},
  47.     {"eval",        Tcl_EvalCmd},
  48.     {"expr",        Tcl_ExprCmd},
  49.     {"for",        Tcl_ForCmd},
  50.     {"foreach",        Tcl_ForeachCmd},
  51.     {"format",        Tcl_FormatCmd},
  52.     {"global",        Tcl_GlobalCmd},
  53.     {"if",        Tcl_IfCmd},
  54.     {"incr",        Tcl_IncrCmd},
  55.     {"info",        Tcl_InfoCmd},
  56.     {"join",        Tcl_JoinCmd},
  57.     {"lappend",        Tcl_LappendCmd},
  58.     {"lindex",        Tcl_LindexCmd},
  59.     {"linsert",        Tcl_LinsertCmd},
  60.     {"list",        Tcl_ListCmd},
  61.     {"llength",        Tcl_LlengthCmd},
  62.     {"lrange",        Tcl_LrangeCmd},
  63.     {"lreplace",    Tcl_LreplaceCmd},
  64.     {"lsearch",        Tcl_LsearchCmd},
  65.     {"lsort",        Tcl_LsortCmd},
  66.     {"proc",        Tcl_ProcCmd},
  67.     {"regexp",        Tcl_RegexpCmd},
  68.     {"regsub",        Tcl_RegsubCmd},
  69.     {"rename",        Tcl_RenameCmd},
  70.     {"return",        Tcl_ReturnCmd},
  71.     {"scan",        Tcl_ScanCmd},
  72.     {"set",        Tcl_SetCmd},
  73.     {"split",        Tcl_SplitCmd},
  74.     {"string",        Tcl_StringCmd},
  75.     {"trace",        Tcl_TraceCmd},
  76.     {"unset",        Tcl_UnsetCmd},
  77.     {"uplevel",        Tcl_UplevelCmd},
  78.     {"upvar",        Tcl_UpvarCmd},
  79.     {"while",        Tcl_WhileCmd},
  80.  
  81.     /*
  82.      * Commands in the UNIX core:
  83.      */
  84.  
  85. #ifndef TCL_GENERIC_ONLY
  86.     {"cd",        Tcl_CdCmd},
  87.     {"close",        Tcl_CloseCmd},
  88.     {"eof",        Tcl_EofCmd},
  89.     {"exec",        Tcl_ExecCmd},
  90.     {"exit",        Tcl_ExitCmd},
  91.     {"file",        Tcl_FileCmd},
  92.     {"flush",        Tcl_FlushCmd},
  93.     {"gets",        Tcl_GetsCmd},
  94.     {"glob",        Tcl_GlobCmd},
  95.     {"open",        Tcl_OpenCmd},
  96.     {"puts",        Tcl_PutsCmd},
  97.     {"pwd",        Tcl_PwdCmd},
  98.     {"read",        Tcl_ReadCmd},
  99.     {"seek",        Tcl_SeekCmd},
  100.     {"source",        Tcl_SourceCmd},
  101.     {"tell",        Tcl_TellCmd},
  102.     {"time",        Tcl_TimeCmd},
  103. #endif /* TCL_GENERIC_ONLY */
  104.     {NULL,        (Tcl_CmdProc *) NULL}
  105. };
  106.  
  107. /*
  108.  *----------------------------------------------------------------------
  109.  *
  110.  * Tcl_CreateInterp --
  111.  *
  112.  *    Create a new TCL command interpreter.
  113.  *
  114.  * Results:
  115.  *    The return value is a token for the interpreter, which may be
  116.  *    used in calls to procedures like Tcl_CreateCmd, Tcl_Eval, or
  117.  *    Tcl_DeleteInterp.
  118.  *
  119.  * Side effects:
  120.  *    The command interpreter is initialized with an empty variable
  121.  *    table and the built-in commands.
  122.  *
  123.  *----------------------------------------------------------------------
  124.  */
  125.  
  126. Tcl_Interp *
  127. Tcl_CreateInterp()
  128. {
  129.     register Interp *iPtr;
  130.     register Command *cmdPtr;
  131.     register CmdInfo *cmdInfoPtr;
  132.     int i;
  133.  
  134.     iPtr = (Interp *) ckalloc(sizeof(Interp));
  135.     iPtr->result = iPtr->resultSpace;
  136.     iPtr->freeProc = 0;
  137.     iPtr->errorLine = 0;
  138.     Tcl_InitHashTable(&iPtr->commandTable, TCL_STRING_KEYS);
  139.     Tcl_InitHashTable(&iPtr->globalTable, TCL_STRING_KEYS);
  140.     iPtr->numLevels = 0;
  141.     iPtr->framePtr = NULL;
  142.     iPtr->varFramePtr = NULL;
  143.     iPtr->activeTracePtr = NULL;
  144.     iPtr->numEvents = 0;
  145.     iPtr->events = NULL;
  146.     iPtr->curEvent = 0;
  147.     iPtr->curEventNum = 0;
  148.     iPtr->revPtr = NULL;
  149.     iPtr->historyFirst = NULL;
  150.     iPtr->revDisables = 1;
  151.     iPtr->evalFirst = iPtr->evalLast = NULL;
  152.     iPtr->appendResult = NULL;
  153.     iPtr->appendAvl = 0;
  154.     iPtr->appendUsed = 0;
  155.     iPtr->numFiles = 0;
  156.     iPtr->filePtrArray = NULL;
  157.     for (i = 0; i < NUM_REGEXPS; i++) {
  158.     iPtr->patterns[i] = NULL;
  159.     iPtr->patLengths[i] = -1;
  160.     iPtr->regexps[i] = NULL;
  161.     }
  162.     iPtr->cmdCount = 0;
  163.     iPtr->noEval = 0;
  164.     iPtr->scriptFile = NULL;
  165.     iPtr->flags = 0;
  166.     iPtr->tracePtr = NULL;
  167.     iPtr->resultSpace[0] = 0;
  168.  
  169.     /*
  170.      * Create the built-in commands.  Do it here, rather than calling
  171.      * Tcl_CreateCommand, because it's faster (there's no need to
  172.      * check for a pre-existing command by the same name).
  173.      */
  174.  
  175.     for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
  176.     int new;
  177.     Tcl_HashEntry *hPtr;
  178.  
  179.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable,
  180.         cmdInfoPtr->name, &new);
  181.     if (new) {
  182.         cmdPtr = (Command *) ckalloc(sizeof(Command));
  183.         cmdPtr->proc = cmdInfoPtr->proc;
  184.         cmdPtr->clientData = (ClientData) NULL;
  185.         cmdPtr->deleteProc = NULL;
  186.         Tcl_SetHashValue(hPtr, cmdPtr);
  187.     }
  188.     }
  189.  
  190. #ifndef TCL_GENERIC_ONLY
  191.     TclSetupEnv((Tcl_Interp *) iPtr);
  192. #endif
  193.  
  194.     return (Tcl_Interp *) iPtr;
  195. }
  196.  
  197. /*
  198.  *----------------------------------------------------------------------
  199.  *
  200.  * Tcl_DeleteInterp --
  201.  *
  202.  *    Delete an interpreter and free up all of the resources associated
  203.  *    with it.
  204.  *
  205.  * Results:
  206.  *    None.
  207.  *
  208.  * Side effects:
  209.  *    The interpreter is destroyed.  The caller should never again
  210.  *    use the interp token.
  211.  *
  212.  *----------------------------------------------------------------------
  213.  */
  214.  
  215. void
  216. Tcl_DeleteInterp(interp)
  217.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  218.                  * by a previous call to Tcl_CreateInterp). */
  219. {
  220.     Interp *iPtr = (Interp *) interp;
  221.     Tcl_HashEntry *hPtr;
  222.     Tcl_HashSearch search;
  223.     register Command *cmdPtr;
  224.     int i;
  225.  
  226.     /*
  227.      * If the interpreter is in use, delay the deletion until later.
  228.      */
  229.  
  230.     iPtr->flags |= DELETED;
  231.     if (iPtr->numLevels != 0) {
  232.     return;
  233.     }
  234.  
  235.     /*
  236.      * Free up any remaining resources associated with the
  237.      * interpreter.
  238.      */
  239.  
  240.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  241.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  242.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  243.     if (cmdPtr->deleteProc != NULL) { 
  244.         (*cmdPtr->deleteProc)(cmdPtr->clientData);
  245.     }
  246.     ckfree((char *) cmdPtr);
  247.     }
  248.     Tcl_DeleteHashTable(&iPtr->commandTable);
  249.     TclDeleteVars(iPtr, &iPtr->globalTable);
  250.     if (iPtr->events != NULL) {
  251.     int i;
  252.  
  253.     for (i = 0; i < iPtr->numEvents; i++) {
  254.         ckfree(iPtr->events[i].command);
  255.     }
  256.     ckfree((char *) iPtr->events);
  257.     }
  258.     while (iPtr->revPtr != NULL) {
  259.     HistoryRev *nextPtr = iPtr->revPtr->nextPtr;
  260.  
  261.     ckfree((char *) iPtr->revPtr);
  262.     iPtr->revPtr = nextPtr;
  263.     }
  264.     if (iPtr->appendResult != NULL) {
  265.     ckfree(iPtr->appendResult);
  266.     }
  267. #ifndef TCL_GENERIC_ONLY
  268.     if (iPtr->numFiles > 0) {
  269.     for (i = 0; i < iPtr->numFiles; i++) {
  270.         OpenFile *filePtr;
  271.     
  272.         filePtr = iPtr->filePtrArray[i];
  273.         if (filePtr == NULL) {
  274.         continue;
  275.         }
  276.         if (i >= 3) {
  277.         fclose(filePtr->f);
  278.         if (filePtr->f2 != NULL) {
  279.             fclose(filePtr->f2);
  280.         }
  281.         if (filePtr->numPids > 0) {
  282.             Tcl_DetachPids(filePtr->numPids, filePtr->pidPtr);
  283.             ckfree((char *) filePtr->pidPtr);
  284.         }
  285.         }
  286.         ckfree((char *) filePtr);
  287.     }
  288.     ckfree((char *) iPtr->filePtrArray);
  289.     }
  290. #endif
  291.     for (i = 0; i < NUM_REGEXPS; i++) {
  292.     if (iPtr->patterns[i] == NULL) {
  293.         break;
  294.     }
  295.     ckfree(iPtr->patterns[i]);
  296.     ckfree((char *) iPtr->regexps[i]);
  297.     }
  298.     while (iPtr->tracePtr != NULL) {
  299.     Trace *nextPtr = iPtr->tracePtr->nextPtr;
  300.  
  301.     ckfree((char *) iPtr->tracePtr);
  302.     iPtr->tracePtr = nextPtr;
  303.     }
  304.     ckfree((char *) iPtr);
  305. }
  306.  
  307. /*
  308.  *----------------------------------------------------------------------
  309.  *
  310.  * Tcl_CreateCommand --
  311.  *
  312.  *    Define a new command in a command table.
  313.  *
  314.  * Results:
  315.  *    None.
  316.  *
  317.  * Side effects:
  318.  *    If a command named cmdName already exists for interp, it is
  319.  *    deleted.  In the future, when cmdName is seen as the name of
  320.  *    a command by Tcl_Eval, proc will be called.  When the command
  321.  *    is deleted from the table, deleteProc will be called.  See the
  322.  *    manual entry for details on the calling sequence.
  323.  *
  324.  *----------------------------------------------------------------------
  325.  */
  326.  
  327. void
  328. Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
  329.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  330.                  * by a previous call to Tcl_CreateInterp). */
  331.     char *cmdName;        /* Name of command. */
  332.     Tcl_CmdProc *proc;        /* Command procedure to associate with
  333.                  * cmdName. */
  334.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  335.     Tcl_CmdDeleteProc *deleteProc;
  336.                 /* If not NULL, gives a procedure to call when
  337.                  * this command is deleted. */
  338. {
  339.     Interp *iPtr = (Interp *) interp;
  340.     register Command *cmdPtr;
  341.     Tcl_HashEntry *hPtr;
  342.     int new;
  343.  
  344.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable, cmdName, &new);
  345.     if (!new) {
  346.     /*
  347.      * Command already exists:  delete the old one.
  348.      */
  349.  
  350.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  351.     if (cmdPtr->deleteProc != NULL) {
  352.         (*cmdPtr->deleteProc)(cmdPtr->clientData);
  353.     }
  354.     } else {
  355.     cmdPtr = (Command *) ckalloc(sizeof(Command));
  356.     Tcl_SetHashValue(hPtr, cmdPtr);
  357.     }
  358.     cmdPtr->proc = proc;
  359.     cmdPtr->clientData = clientData;
  360.     cmdPtr->deleteProc = deleteProc;
  361. }
  362.  
  363. /*
  364.  *----------------------------------------------------------------------
  365.  *
  366.  * Tcl_DeleteCommand --
  367.  *
  368.  *    Remove the given command from the given interpreter.
  369.  *
  370.  * Results:
  371.  *    0 is returned if the command was deleted successfully.
  372.  *    -1 is returned if there didn't exist a command by that
  373.  *    name.
  374.  *
  375.  * Side effects:
  376.  *    CmdName will no longer be recognized as a valid command for
  377.  *    interp.
  378.  *
  379.  *----------------------------------------------------------------------
  380.  */
  381.  
  382. int
  383. Tcl_DeleteCommand(interp, cmdName)
  384.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  385.                  * by a previous call to Tcl_CreateInterp). */
  386.     char *cmdName;        /* Name of command to remove. */
  387. {
  388.     Interp *iPtr = (Interp *) interp;
  389.     Tcl_HashEntry *hPtr;
  390.     Command *cmdPtr;
  391.  
  392.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, cmdName);
  393.     if (hPtr == NULL) {
  394.     return -1;
  395.     }
  396.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  397.     if (cmdPtr->deleteProc != NULL) {
  398.     (*cmdPtr->deleteProc)(cmdPtr->clientData);
  399.     }
  400.     ckfree((char *) cmdPtr);
  401.     Tcl_DeleteHashEntry(hPtr);
  402.     return 0;
  403. }
  404.  
  405. /*
  406.  *-----------------------------------------------------------------
  407.  *
  408.  * Tcl_Eval --
  409.  *
  410.  *    Parse and execute a command in the Tcl language.
  411.  *
  412.  * Results:
  413.  *    The return value is one of the return codes defined in tcl.hd
  414.  *    (such as TCL_OK), and interp->result contains a string value
  415.  *    to supplement the return code.  The value of interp->result
  416.  *    will persist only until the next call to Tcl_Eval:  copy it or
  417.  *    lose it! *TermPtr is filled in with the character just after
  418.  *    the last one that was part of the command (usually a NULL
  419.  *    character or a closing bracket).
  420.  *
  421.  * Side effects:
  422.  *    Almost certainly;  depends on the command.
  423.  *
  424.  *-----------------------------------------------------------------
  425.  */
  426.  
  427. int
  428. Tcl_Eval(interp, cmd, flags, termPtr)
  429.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  430.                  * by a previous call to Tcl_CreateInterp). */
  431.     char *cmd;            /* Pointer to TCL command to interpret. */
  432.     int flags;            /* OR-ed combination of flags like
  433.                  * TCL_BRACKET_TERM and TCL_RECORD_BOUNDS. */
  434.     char **termPtr;        /* If non-NULL, fill in the address it points
  435.                  * to with the address of the char. just after
  436.                  * the last one that was part of cmd.  See
  437.                  * the man page for details on this. */
  438. {
  439.     /*
  440.      * The storage immediately below is used to generate a copy
  441.      * of the command, after all argument substitutions.  Pv will
  442.      * contain the argv values passed to the command procedure.
  443.      */
  444.  
  445. #   define NUM_CHARS 200
  446.     char copyStorage[NUM_CHARS];
  447.     ParseValue pv;
  448.     char *oldBuffer;
  449.  
  450.     /*
  451.      * This procedure generates an (argv, argc) array for the command,
  452.      * It starts out with stack-allocated space but uses dynamically-
  453.      * allocated storage to increase it if needed.
  454.      */
  455.  
  456. #   define NUM_ARGS 10
  457.     char *(argStorage[NUM_ARGS]);
  458.     char **argv = argStorage;
  459.     int argc;
  460.     int argSize = NUM_ARGS;
  461.  
  462.     register char *src;            /* Points to current character
  463.                      * in cmd. */
  464.     char termChar;            /* Return when this character is found
  465.                      * (either ']' or '\0').  Zero means
  466.                      * that newlines terminate commands. */
  467.     int result;                /* Return value. */
  468.     register Interp *iPtr = (Interp *) interp;
  469.     Tcl_HashEntry *hPtr;
  470.     Command *cmdPtr;
  471.     char *dummy;            /* Make termPtr point here if it was
  472.                      * originally NULL. */
  473.     char *cmdStart;            /* Points to first non-blank char. in
  474.                      * command (used in calling trace
  475.                      * procedures). */
  476.     char *ellipsis = "";        /* Used in setting errorInfo variable;
  477.                      * set to "..." to indicate that not
  478.                      * all of offending command is included
  479.                      * in errorInfo.  "" means that the
  480.                      * command is all there. */
  481.     register Trace *tracePtr;
  482.  
  483.     /*
  484.      * Initialize the result to an empty string and clear out any
  485.      * error information.  This makes sure that we return an empty
  486.      * result if there are no commands in the command string.
  487.      */
  488.  
  489.     Tcl_FreeResult((Tcl_Interp *) iPtr);
  490.     iPtr->result = iPtr->resultSpace;
  491.     iPtr->resultSpace[0] = 0;
  492.     result = TCL_OK;
  493.  
  494.     /*
  495.      * Check depth of nested calls to Tcl_Eval:  if this gets too large,
  496.      * it's probably because of an infinite loop somewhere.
  497.      */
  498.  
  499.     iPtr->numLevels++;
  500.     if (iPtr->numLevels > MAX_NESTING_DEPTH) {
  501.     iPtr->numLevels--;
  502.     iPtr->result =  "too many nested calls to Tcl_Eval (infinite loop?)";
  503.     return TCL_ERROR;
  504.     }
  505.  
  506.     /*
  507.      * Initialize the area in which command copies will be assembled.
  508.      */
  509.  
  510.     pv.buffer = copyStorage;
  511.     pv.end = copyStorage + NUM_CHARS - 1;
  512.     pv.expandProc = TclExpandParseValue;
  513.     pv.clientData = (ClientData) NULL;
  514.  
  515.     src = cmd;
  516.     if (flags & TCL_BRACKET_TERM) {
  517.     termChar = ']';
  518.     } else {
  519.     termChar = 0;
  520.     }
  521.     if (termPtr == NULL) {
  522.     termPtr = &dummy;
  523.     }
  524.     *termPtr = src;
  525.     cmdStart = src;
  526.  
  527.     /*
  528.      * There can be many sub-commands (separated by semi-colons or
  529.      * newlines) in one command string.  This outer loop iterates over
  530.      * individual commands.
  531.      */
  532.  
  533.     while (*src != termChar) {
  534.     iPtr->flags &= ~(ERR_IN_PROGRESS | ERROR_CODE_SET);
  535.  
  536.     /*
  537.      * Skim off leading white space and semi-colons, and skip
  538.      * comments.
  539.      */
  540.  
  541.     while (1) {
  542.         register char c = *src;
  543.  
  544.         if ((CHAR_TYPE(c) != TCL_SPACE) && (c != ';') && (c != '\n')) {
  545.         break;
  546.         }
  547.         src += 1;
  548.     }
  549.     if (*src == '#') {
  550.         for (src++; *src != 0; src++) {
  551.         if ((*src == '\n') && (src[-1] != '\\')) {
  552.             src++;
  553.             break;
  554.         }
  555.         }
  556.         continue;
  557.     }
  558.     cmdStart = src;
  559.  
  560.     /*
  561.      * Parse the words of the command, generating the argc and
  562.      * argv for the command procedure.  May have to call
  563.      * TclParseWords several times, expanding the argv array
  564.      * between calls.
  565.      */
  566.  
  567.     pv.next = oldBuffer = pv.buffer;
  568.     argc = 0;
  569.     while (1) {
  570.         int newArgs, maxArgs;
  571.         char **newArgv;
  572.         int i;
  573.  
  574.         /*
  575.          * Note:  the "- 2" below guarantees that we won't use the
  576.          * last two argv slots here.  One is for a NULL pointer to
  577.          * mark the end of the list, and the other is to leave room
  578.          * for inserting the command name "unknown" as the first
  579.          * argument (see below).
  580.          */
  581.  
  582.         maxArgs = argSize - argc - 2;
  583.         result = TclParseWords((Tcl_Interp *) iPtr, src, flags,
  584.             maxArgs, termPtr, &newArgs, &argv[argc], &pv);
  585.         src = *termPtr;
  586.         if (result != TCL_OK) {
  587.         ellipsis = "...";
  588.         goto done;
  589.         }
  590.  
  591.         /*
  592.          * Careful!  Buffer space may have gotten reallocated while
  593.          * parsing words.  If this happened, be sure to update all
  594.          * of the older argv pointers to refer to the new space.
  595.          */
  596.  
  597.         if (oldBuffer != pv.buffer) {
  598.         int i;
  599.  
  600.         for (i = 0; i < argc; i++) {
  601.             argv[i] = pv.buffer + (argv[i] - oldBuffer);
  602.         }
  603.         oldBuffer = pv.buffer;
  604.         }
  605.         argc += newArgs;
  606.         if (newArgs < maxArgs) {
  607.         argv[argc] = (char *) NULL;
  608.         break;
  609.         }
  610.  
  611.         /*
  612.          * Args didn't all fit in the current array.  Make it bigger.
  613.          */
  614.  
  615.         argSize *= 2;
  616.         newArgv = (char **)
  617.             ckalloc((unsigned) argSize * sizeof(char *));
  618.         for (i = 0; i < argc; i++) {
  619.         newArgv[i] = argv[i];
  620.         }
  621.         if (argv != argStorage) {
  622.         ckfree((char *) argv);
  623.         }
  624.         argv = newArgv;
  625.     }
  626.  
  627.     /*
  628.      * If this is an empty command (or if we're just parsing
  629.      * commands without evaluating them), then just skip to the
  630.      * next command.
  631.      */
  632.  
  633.     if ((argc == 0) || iPtr->noEval) {
  634.         continue;
  635.     }
  636.     argv[argc] = NULL;
  637.  
  638.     /*
  639.      * Save information for the history module, if needed.
  640.      */
  641.  
  642.     if (flags & TCL_RECORD_BOUNDS) {
  643.         iPtr->evalFirst = cmdStart;
  644.         iPtr->evalLast = src-1;
  645.     }
  646.  
  647.     /*
  648.      * Find the procedure to execute this command.  If there isn't
  649.      * one, then see if there is a command "unknown".  If so,
  650.      * invoke it instead, passing it the words of the original
  651.      * command as arguments.
  652.      */
  653.  
  654.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, argv[0]);
  655.     if (hPtr == NULL) {
  656.         int i;
  657.  
  658.         hPtr = Tcl_FindHashEntry(&iPtr->commandTable, "unknown");
  659.         if (hPtr == NULL) {
  660.         Tcl_ResetResult(interp);
  661.         Tcl_AppendResult(interp, "invalid command name: \"",
  662.             argv[0], "\"", (char *) NULL);
  663.         result = TCL_ERROR;
  664.         goto done;
  665.         }
  666.         for (i = argc; i >= 0; i--) {
  667.         argv[i+1] = argv[i];
  668.         }
  669.         argv[0] = "unknown";
  670.         argc++;
  671.     }
  672.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  673.  
  674.     /*
  675.      * Call trace procedures, if any.
  676.      */
  677.  
  678.     for (tracePtr = iPtr->tracePtr; tracePtr != NULL;
  679.         tracePtr = tracePtr->nextPtr) {
  680.         char saved;
  681.  
  682.         if (tracePtr->level < iPtr->numLevels) {
  683.         continue;
  684.         }
  685.         saved = *src;
  686.         *src = 0;
  687.         (*tracePtr->proc)(tracePtr->clientData, interp, iPtr->numLevels,
  688.             cmdStart, cmdPtr->proc, cmdPtr->clientData, argc, argv);
  689.         *src = saved;
  690.     }
  691.  
  692.     /*
  693.      * At long last, invoke the command procedure.  Reset the
  694.      * result to its default empty value first (it could have
  695.      * gotten changed by earlier commands in the same command
  696.      * string).
  697.      */
  698.  
  699.     iPtr->cmdCount++;
  700.     Tcl_FreeResult(iPtr);
  701.     iPtr->result = iPtr->resultSpace;
  702.     iPtr->resultSpace[0] = 0;
  703.     result = (*cmdPtr->proc)(cmdPtr->clientData, interp, argc, argv);
  704.     if (result != TCL_OK) {
  705.         break;
  706.     }
  707.     }
  708.  
  709.     /*
  710.      * Free up any extra resources that were allocated.
  711.      */
  712.  
  713.     done:
  714.     if (pv.buffer != copyStorage) {
  715.     ckfree((char *) pv.buffer);
  716.     }
  717.     if (argv != argStorage) {
  718.     ckfree((char *) argv);
  719.     }
  720.     iPtr->numLevels--;
  721.     if (iPtr->numLevels == 0) {
  722.     if (result == TCL_RETURN) {
  723.         result = TCL_OK;
  724.     }
  725.     if ((result != TCL_OK) && (result != TCL_ERROR)) {
  726.         Tcl_ResetResult(interp);
  727.         if (result == TCL_BREAK) {
  728.         iPtr->result = "invoked \"break\" outside of a loop";
  729.         } else if (result == TCL_CONTINUE) {
  730.         iPtr->result = "invoked \"continue\" outside of a loop";
  731.         } else {
  732.         iPtr->result = iPtr->resultSpace;
  733.         sprintf(iPtr->resultSpace, "command returned bad code: %d",
  734.             result);
  735.         }
  736.         result = TCL_ERROR;
  737.     }
  738.     if (iPtr->flags & DELETED) {
  739.         Tcl_DeleteInterp(interp);
  740.     }
  741.     }
  742.  
  743.     /*
  744.      * If an error occurred, record information about what was being
  745.      * executed when the error occurred.
  746.      */
  747.  
  748.     if ((result == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
  749.     int numChars;
  750.     register char *p;
  751.  
  752.     /*
  753.      * Compute the line number where the error occurred.
  754.      */
  755.  
  756.     iPtr->errorLine = 1;
  757.     for (p = cmd; p != cmdStart; p++) {
  758.         if (*p == '\n') {
  759.         iPtr->errorLine++;
  760.         }
  761.     }
  762.     for ( ; isspace(*p) || (*p == ';'); p++) {
  763.         if (*p == '\n') {
  764.         iPtr->errorLine++;
  765.         }
  766.     }
  767.  
  768.     /*
  769.      * Figure out how much of the command to print in the error
  770.      * message (up to a certain number of characters, or up to
  771.      * the first new-line).
  772.      */
  773.  
  774.     numChars = src - cmdStart;
  775.     if (numChars > (NUM_CHARS-50)) {
  776.         numChars = NUM_CHARS-50;
  777.         ellipsis = " ...";
  778.     }
  779.  
  780.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  781.         sprintf(copyStorage, "\n    while executing\n\"%.*s%s\"",
  782.             numChars, cmdStart, ellipsis);
  783.     } else {
  784.         sprintf(copyStorage, "\n    invoked from within\n\"%.*s%s\"",
  785.             numChars, cmdStart, ellipsis);
  786.     }
  787.     Tcl_AddErrorInfo(interp, copyStorage);
  788.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  789.     } else {
  790.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  791.     }
  792.     return result;
  793. }
  794.  
  795. /*
  796.  *----------------------------------------------------------------------
  797.  *
  798.  * Tcl_CreateTrace --
  799.  *
  800.  *    Arrange for a procedure to be called to trace command execution.
  801.  *
  802.  * Results:
  803.  *    The return value is a token for the trace, which may be passed
  804.  *    to Tcl_DeleteTrace to eliminate the trace.
  805.  *
  806.  * Side effects:
  807.  *    From now on, proc will be called just before a command procedure
  808.  *    is called to execute a Tcl command.  Calls to proc will have the
  809.  *    following form:
  810.  *
  811.  *    void
  812.  *    proc(clientData, interp, level, command, cmdProc, cmdClientData,
  813.  *        argc, argv)
  814.  *        ClientData clientData;
  815.  *        Tcl_Interp *interp;
  816.  *        int level;
  817.  *        char *command;
  818.  *        int (*cmdProc)();
  819.  *        ClientData cmdClientData;
  820.  *        int argc;
  821.  *        char **argv;
  822.  *    {
  823.  *    }
  824.  *
  825.  *    The clientData and interp arguments to proc will be the same
  826.  *    as the corresponding arguments to this procedure.  Level gives
  827.  *    the nesting level of command interpretation for this interpreter
  828.  *    (0 corresponds to top level).  Command gives the ASCII text of
  829.  *    the raw command, cmdProc and cmdClientData give the procedure that
  830.  *    will be called to process the command and the ClientData value it
  831.  *    will receive, and argc and argv give the arguments to the
  832.  *    command, after any argument parsing and substitution.  Proc
  833.  *    does not return a value.
  834.  *
  835.  *----------------------------------------------------------------------
  836.  */
  837.  
  838. Tcl_Trace
  839. Tcl_CreateTrace(interp, level, proc, clientData)
  840.     Tcl_Interp *interp;        /* Interpreter in which to create the trace. */
  841.     int level;            /* Only call proc for commands at nesting level
  842.                  * <= level (1 => top level). */
  843.     Tcl_CmdTraceProc *proc;    /* Procedure to call before executing each
  844.                  * command. */
  845.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  846. {
  847.     register Trace *tracePtr;
  848.     register Interp *iPtr = (Interp *) interp;
  849.  
  850.     tracePtr = (Trace *) ckalloc(sizeof(Trace));
  851.     tracePtr->level = level;
  852.     tracePtr->proc = proc;
  853.     tracePtr->clientData = clientData;
  854.     tracePtr->nextPtr = iPtr->tracePtr;
  855.     iPtr->tracePtr = tracePtr;
  856.  
  857.     return (Tcl_Trace) tracePtr;
  858. }
  859.  
  860. /*
  861.  *----------------------------------------------------------------------
  862.  *
  863.  * Tcl_DeleteTrace --
  864.  *
  865.  *    Remove a trace.
  866.  *
  867.  * Results:
  868.  *    None.
  869.  *
  870.  * Side effects:
  871.  *    From now on there will be no more calls to the procedure given
  872.  *    in trace.
  873.  *
  874.  *----------------------------------------------------------------------
  875.  */
  876.  
  877. void
  878. Tcl_DeleteTrace(interp, trace)
  879.     Tcl_Interp *interp;        /* Interpreter that contains trace. */
  880.     Tcl_Trace trace;        /* Token for trace (returned previously by
  881.                  * Tcl_CreateTrace). */
  882. {
  883.     register Interp *iPtr = (Interp *) interp;
  884.     register Trace *tracePtr = (Trace *) trace;
  885.     register Trace *tracePtr2;
  886.  
  887.     if (iPtr->tracePtr == tracePtr) {
  888.     iPtr->tracePtr = tracePtr->nextPtr;
  889.     ckfree((char *) tracePtr);
  890.     } else {
  891.     for (tracePtr2 = iPtr->tracePtr; tracePtr2 != NULL;
  892.         tracePtr2 = tracePtr2->nextPtr) {
  893.         if (tracePtr2->nextPtr == tracePtr) {
  894.         tracePtr2->nextPtr = tracePtr->nextPtr;
  895.         ckfree((char *) tracePtr);
  896.         return;
  897.         }
  898.     }
  899.     }
  900. }
  901.  
  902. /*
  903.  *----------------------------------------------------------------------
  904.  *
  905.  * Tcl_AddErrorInfo --
  906.  *
  907.  *    Add information to a message being accumulated that describes
  908.  *    the current error.
  909.  *
  910.  * Results:
  911.  *    None.
  912.  *
  913.  * Side effects:
  914.  *    The contents of message are added to the "errorInfo" variable.
  915.  *    If Tcl_Eval has been called since the current value of errorInfo
  916.  *    was set, errorInfo is cleared before adding the new message.
  917.  *
  918.  *----------------------------------------------------------------------
  919.  */
  920.  
  921. void
  922. Tcl_AddErrorInfo(interp, message)
  923.     Tcl_Interp *interp;        /* Interpreter to which error information
  924.                  * pertains. */
  925.     char *message;        /* Message to record. */
  926. {
  927.     register Interp *iPtr = (Interp *) interp;
  928.  
  929.     /*
  930.      * If an error is already being logged, then the new errorInfo
  931.      * is the concatenation of the old info and the new message.
  932.      * If this is the first piece of info for the error, then the
  933.      * new errorInfo is the concatenation of the message in
  934.      * interp->result and the new message.
  935.      */
  936.  
  937.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  938.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, interp->result,
  939.         TCL_GLOBAL_ONLY);
  940.     iPtr->flags |= ERR_IN_PROGRESS;
  941.  
  942.     /*
  943.      * If the errorCode variable wasn't set by the code that generated
  944.      * the error, set it to "NONE".
  945.      */
  946.  
  947.     if (!(iPtr->flags & ERROR_CODE_SET)) {
  948.         (void) Tcl_SetVar2(interp, "errorCode", (char *) NULL, "NONE",
  949.             TCL_GLOBAL_ONLY);
  950.     }
  951.     }
  952.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, message,
  953.         TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
  954. }
  955.  
  956. /*
  957.  *----------------------------------------------------------------------
  958.  *
  959.  * Tcl_VarEval --
  960.  *
  961.  *    Given a variable number of string arguments, concatenate them
  962.  *    all together and execute the result as a Tcl command.
  963.  *
  964.  * Results:
  965.  *    A standard Tcl return result.  An error message or other
  966.  *    result may be left in interp->result.
  967.  *
  968.  * Side effects:
  969.  *    Depends on what was done by the command.
  970.  *
  971.  *----------------------------------------------------------------------
  972.  */
  973.     /* VARARGS2 */ /* ARGSUSED */
  974. int
  975. #ifndef lint
  976. Tcl_VarEval(va_alist)
  977. #else
  978. Tcl_VarEval(iPtr, p, va_alist)
  979.     Tcl_Interp *iPtr;        /* Interpreter in which to execute command. */
  980.     char *p;            /* One or more strings to concatenate,
  981.                  * terminated with a NULL string. */
  982. #endif
  983.     va_dcl
  984. {
  985.     va_list argList;
  986. #define FIXED_SIZE 200
  987.     char fixedSpace[FIXED_SIZE+1];
  988.     int spaceAvl, spaceUsed, length;
  989.     char *string, *cmd;
  990.     Tcl_Interp *interp;
  991.     int result;
  992.  
  993.     /*
  994.      * Copy the strings one after the other into a single larger
  995.      * string.  Use stack-allocated space for small commands, but if
  996.      * the commands gets too large than call ckalloc to create the
  997.      * space.
  998.      */
  999.  
  1000.     va_start(argList);
  1001.     interp = va_arg(argList, Tcl_Interp *);
  1002.     spaceAvl = FIXED_SIZE;
  1003.     spaceUsed = 0;
  1004.     cmd = fixedSpace;
  1005.     while (1) {
  1006.     string = va_arg(argList, char *);
  1007.     if (string == NULL) {
  1008.         break;
  1009.     }
  1010.     length = strlen(string);
  1011.     if ((spaceUsed + length) > spaceAvl) {
  1012.         char *new;
  1013.  
  1014.         spaceAvl = spaceUsed + length;
  1015.         spaceAvl += spaceAvl/2;
  1016.         new = ckalloc((unsigned) spaceAvl);
  1017.         memcpy((VOID *) new, (VOID *) cmd, spaceUsed);
  1018.         if (cmd != fixedSpace) {
  1019.         ckfree(cmd);
  1020.         }
  1021.         cmd = new;
  1022.     }
  1023.     strcpy(cmd + spaceUsed, string);
  1024.     spaceUsed += length;
  1025.     }
  1026.     va_end(argList);
  1027.     cmd[spaceUsed] = '\0';
  1028.  
  1029.     result = Tcl_Eval(interp, cmd, 0, (char **) NULL);
  1030.     if (cmd != fixedSpace) {
  1031.     ckfree(cmd);
  1032.     }
  1033.     return result;
  1034. }
  1035.  
  1036. /*
  1037.  *----------------------------------------------------------------------
  1038.  *
  1039.  * Tcl_GlobalEval --
  1040.  *
  1041.  *    Evaluate a command at global level in an interpreter.
  1042.  *
  1043.  * Results:
  1044.  *    A standard Tcl result is returned, and interp->result is
  1045.  *    modified accordingly.
  1046.  *
  1047.  * Side effects:
  1048.  *    The command string is executed in interp, and the execution
  1049.  *    is carried out in the variable context of global level (no
  1050.  *    procedures active), just as if an "uplevel #0" command were
  1051.  *    being executed.
  1052.  *
  1053.  *----------------------------------------------------------------------
  1054.  */
  1055.  
  1056. int
  1057. Tcl_GlobalEval(interp, command)
  1058.     Tcl_Interp *interp;        /* Interpreter in which to evaluate command. */
  1059.     char *command;        /* Command to evaluate. */
  1060. {
  1061.     register Interp *iPtr = (Interp *) interp;
  1062.     int result;
  1063.     CallFrame *savedVarFramePtr;
  1064.  
  1065.     savedVarFramePtr = iPtr->varFramePtr;
  1066.     iPtr->varFramePtr = NULL;
  1067.     result = Tcl_Eval(interp, command, 0, (char **) NULL);
  1068.     iPtr->varFramePtr = savedVarFramePtr;
  1069.     return result;
  1070. }
  1071.